package com.example.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.dao.PasswdDao;
import com.example.domain.Passwd;
import com.example.result.Result;
import com.example.result.ResultFactory;
import com.example.service.PasswdService;
import com.example.tool.DateTool;
import com.example.tool.MD5Utils;
import com.example.tool.PasswordTool;

@Service
public class PasswdServiceImpl implements PasswdService {

    @Autowired
    PasswdDao passwdDao;

    /**
     * @describe 添加用户密码信息
     */
    @Override
    public Result addPasswd(Passwd passwd) {
        if (passwd != null) {
            // 获取系统当前时间作为记录创建时间
            passwd.setRecordTime(DateTool.getTimestamp());
            passwd.setPassword(MD5Utils.getMD5(passwd.getPassword().getBytes()));
        }
        // 调用JPA保存实体方法
        Passwd passwd1 = passwdDao.save(passwd);

        if (passwd1 != null) {
            return ResultFactory.buildSuccessResult(passwd1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 更新用户密码信息
     */
    @Override
    public Result updatePasswd(Passwd passwd) {
        if (passwd != null) {
            // 获取系统当前时间作为记录更新时间
            passwd.setUpdateTime(DateTool.getTimestamp());
            passwd.setPassword(MD5Utils.getMD5(passwd.getPassword().getBytes()));
        }
        // 调用JPA保存并刷新实体方法
        Passwd passwd1 = passwdDao.saveAndFlush(passwd);

        if (passwd1 != null) {
            return ResultFactory.buildSuccessResult(passwd1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据编号删除用户密码信息
     */
    @Override
    public Result deletePasswdById(String passwdId) {
        int row = passwdDao.deleteByPasswdId(Integer.parseInt(passwdId));

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有用户密码信息
     */
    @Override
    public Result queryAllPasswd() {
        List<Passwd> list = passwdDao.findAll();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有用户账号和用户密码信息
     */
    @Override
    public Result queryAllPasswdAndUser() {

        List<Map<String, Object>> list = passwdDao.queryAllPasswdAndUser();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据用户编号查询该用户密码信息
     */
    @Override
    public Result queryPasswdById(String passwdId) {
        List<Passwd> list = passwdDao.findByPasswdId(Integer.parseInt(passwdId));
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 通过Excel批量添加用户密码信息
     */
    @Override
    public Result addPasswdByExcel(List<Passwd> list) {
        List<Passwd> list1 = null;
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setRecordTime(DateTool.getTimestamp());
                list.get(i).setPassword(MD5Utils.getMD5(list.get(i).getPassword().getBytes()));
            }
            list1 = passwdDao.saveAll(list);
        }
        if (list1 != null) {
            return ResultFactory.buildSuccessResult(list1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据编号批量删除用户密码信息
     */
    @Override
    public Result deletePasswdByIds(Map<String, String> ids) {
        List<Integer> listId = new ArrayList<Integer>();
        int row = 0;
        if (ids != null) {
            String idstr = ids.get("Ids");
            String[] id = idstr.split(",");

            for (int i = 0; i < id.length; i++) {
                listId.add(Integer.parseInt(id[i]));

            }
        }
        System.out.println(listId);
        if (listId != null) {
            row = passwdDao.deleteByPasswdIdIn(listId);
        }

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据查询内容模糊查询用户密码信息
     */
    @Override
    public Result serachByInfo(String[] info) {
        List<Map<String, Object>> list = null;

        if (info[0].equals("userName")) {

            list = passwdDao.findByUserNameLike(info[1]);

        } else if (info[0].equals("nickName")) {
            list = passwdDao.findByNickNameLike(info[1]);
        }

        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据密码编号批量重置密码信息
     */
    @Override
    public Result resetPasswdByIds(String json) {

        JSONObject parseObject = JSON.parseObject(json);
        String[] pids = parseObject.getString("pId").split(",");
        String[] uids = parseObject.getString("uId").split(",");
        String[] times = parseObject.getString("times").split(",");

        List<Passwd> list = passwdDao.saveAll(PasswordTool.resetPasswordByIds(pids, uids, times));

        if (list != null) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

}
